package com.example.bookservice.utils;

import com.example.bookservice.entity.Book;
import com.example.bookservice.entity.Chapter;
import com.example.bookservice.vo.PageResultVo;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 *  处理 本服务 es 的结果解析函数
 */
@Component
public class EsResponseUtils {

    @Autowired
    private ObjectMapper objectMapper;

    /**
     *  处理 书籍DSL response 的函数
     */
    public PageResultVo<?> handelResponse(SearchResponse response) {

        // 数据解析(对照 DSL 语句)
        // 1. 拿到源数据
        SearchHits responseHits = response.getHits();
        // 2. 拿到查询总条数
        long total = responseHits.getTotalHits().value;
        // 3. 拿到数据数组
        SearchHit[] searchHits = responseHits.getHits();
        // 4. 遍历拿到每个 source
        List<Book> books = new ArrayList<>();
        for (SearchHit documentFields : searchHits) {
            // 4.1 拿到 source
            String source = documentFields.getSourceAsString();
            // 4.2 打印结果 (或其他处理)

            Book book;
            try {
                book = objectMapper.readValue(source, Book.class);
            } catch (JsonProcessingException e) {
                throw new RuntimeException(e);
            }
            // 4.3 获取高亮结果集
            Map<String, HighlightField> highlightFields = documentFields.getHighlightFields();
            if (!CollectionUtils.isEmpty(highlightFields)) {
                HighlightField titleHighlightField = highlightFields.get("title");
                if (titleHighlightField != null) {
                    String title = titleHighlightField.getFragments()[0].string();
                    book.setTitle(title);
                }

                HighlightField authorHighlightField = highlightFields.get("author");
                if (authorHighlightField != null) {
                    String author = authorHighlightField.getFragments()[0].string();
                    book.setAuthor(author);
                }
            }

            books.add(book);
        }
        System.out.println("一共查询到条数: " + total);

        return new PageResultVo<>(total, books);
    }

    /**
     *  处理 章节DSL response 的函数
     */
    public PageResultVo<?> handelChapterResponse(SearchResponse response) {

        // 数据解析(对照 DSL 语句)
        // 1. 拿到源数据
        SearchHits responseHits = response.getHits();
        // 2. 拿到查询总条数
        long total = responseHits.getTotalHits().value;
        // 3. 拿到数据数组
        SearchHit[] searchHits = responseHits.getHits();
        // 4. 遍历拿到每个 source
        List<Chapter> chapters = new ArrayList<>();
        for (SearchHit documentFields : searchHits) {
            // 4.1 拿到 source
            String source = documentFields.getSourceAsString();
            // 4.2 打印结果 (或其他处理)
            Chapter chapter;
            try {
                chapter = objectMapper.readValue(source, Chapter.class);
            } catch (JsonProcessingException e) {
                throw new RuntimeException(e);
            }

            chapters.add(chapter);
        }

        System.out.println("一共查询到条数: " + total);

        return new PageResultVo<>(total, chapters);
    }
}
